<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>动态管理 - 测盟汇系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        :root {
            --primary-color: #2c5545;
            --primary-light: #3a7d62;
            --primary-hover: #1e4636;
            --accent-color: #8ba888;
            --dynamic-color: #cce622;
            --text-color: #333;
            --text-light: #555;
            --border-color: #d1d9d6;
            --bg-light: #f5f7f6;
            --bg-page: #e8ecea;
            --white: #fff;
            --shadow: 0 4px 24px rgba(44, 85, 69, 0.1);
            --radius: 12px;
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: var(--bg-page);
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: var(--text-color);
            line-height: 1.6;
        }

        .header {
            background: var(--primary-color);
            color: var(--white);
            padding: 1rem 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .logo {
            display: flex;
            align-items: center;
        }

        .logo i {
            font-size: 1.8rem;
            margin-right: 0.8rem;
        }

        .logo h1 {
            font-size: 1.5rem;
            font-weight: 600;
        }

        .user-info {
            display: flex;
            align-items: center;
        }

        .user-info i {
            font-size: 1.2rem;
            margin-right: 0.5rem;
        }

        .logout-btn {
            background: rgba(255, 255, 255, 0.2);
            border: none;
            color: var(--white);
            padding: 0.5rem 1rem;
            border-radius: 4px;
            cursor: pointer;
            transition: var(--transition);
            margin-left: 1rem;
            display: flex;
            align-items: center;
        }

        .logout-btn i {
            margin-right: 0.5rem;
        }

        .logout-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .container {
            max-width: 1400px;
            margin: 2rem auto;
            padding: 0 2rem;
        }

        .content-card {
            background: var(--white);
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            padding: 2rem;
            margin-bottom: 2rem;
            border: 1px solid var(--border-color);
        }

        .page-title {
            color: var(--primary-color);
            font-size: 1.8rem;
            margin-bottom: 1.5rem;
            display: flex;
            align-items: center;
        }

        .page-title i {
            margin-right: 1rem;
        }

        .action-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
        }

        .btn {
            padding: 0.8rem 1.5rem;
            border-radius: var(--radius);
            border: none;
            cursor: pointer;
            font-weight: 500;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
        }

        .btn i {
            margin-right: 0.5rem;
        }

        .btn-primary {
            background: var(--primary-color);
            color: var(--white);
        }

        .btn-primary:hover {
            background: var(--primary-hover);
            transform: translateY(-2px);
        }

        .btn-secondary {
            background: var(--bg-light);
            color: var(--text-color);
            border: 1px solid var(--border-color);
        }

        .btn-secondary:hover {
            background: #e1e8e4;
            transform: translateY(-2px);
        }

        .btn-dynamic {
            background: var(--dynamic-color);
            color: var(--white);
        }

        .btn-dynamic:hover {
            background: #d35400;
            transform: translateY(-2px);
        }

        .search-container {
            display: flex;
            gap: 1rem;
            align-items: center;
            margin-bottom: 1.5rem;
            flex-wrap: wrap;
        }

        .form-control {
            padding: 0.8rem;
            border: 1px solid var(--border-color);
            border-radius: var(--radius);
            font-size: 1rem;
            background: var(--bg-light);
            transition: var(--transition);
        }

        .form-control:focus {
            border-color: var(--primary-color);
            outline: none;
            box-shadow: 0 0 0 2px rgba(44, 85, 69, 0.2);
            background: var(--white);
        }

        .table {
            width: 100%;
            border-collapse: separate;
            border-spacing: 0;
            margin-bottom: 1.5rem;
        }

        .table thead th {
            background: var(--primary-color);
            color: var(--white);
            padding: 1rem;
            text-align: left;
            font-weight: 500;
        }

        .table tbody tr {
            transition: var(--transition);
        }

        .table tbody tr:hover {
            background: rgba(44, 85, 69, 0.05);
        }

        .table td, .table th {
            padding: 1rem;
            border-bottom: 1px solid var(--border-color);
            vertical-align: middle;
        }

        .cover-image {
            width: 80px;
            height: 60px;
            object-fit: cover;
            border-radius: 6px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            transition: var(--transition);
        }

        .cover-image:hover {
            transform: scale(1.05);
        }

        .status-tag {
            padding: 0.4rem 0.8rem;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 500;
            display: inline-flex;
            align-items: center;
        }

        .status-tag i {
            margin-right: 0.3rem;
            font-size: 0.8rem;
        }

        .status-pending {
            background: #fff3cd;
            color: #856404;
        }

        .status-published {
            background: #d4edda;
            color: #155724;
        }

        .action-buttons {
            display: flex;
            gap: 0.5rem;
        }

        .btn-sm {
            padding: 0.5rem 1rem;
            font-size: 0.85rem;
        }

        .pagination {
            display: flex;
            justify-content: center;
            gap: 0.5rem;
            margin-top: 1.5rem;
        }

        .page-item {
            display: inline-flex;
        }

        .page-link {
            padding: 0.6rem 1rem;
            border-radius: var(--radius);
            border: 1px solid var(--border-color);
            color: var(--text-color);
            text-decoration: none;
            transition: var(--transition);
        }

        .page-link:hover {
            background: var(--bg-light);
        }

        .page-item.active .page-link {
            background: var(--primary-color);
            color: var(--white);
            border-color: var(--primary-color);
        }

        .empty-state {
            text-align: center;
            padding: 3rem;
            color: var(--text-light);
        }

        .empty-state i {
            font-size: 3rem;
            margin-bottom: 1rem;
            color: var(--border-color);
        }

        @media (max-width: 768px) {
            .header {
                flex-direction: column;
                text-align: center;
                padding: 1rem;
            }

            .user-info {
                margin-top: 1rem;
            }

            .action-bar {
                flex-direction: column;
                gap: 1rem;
                align-items: flex-start;
            }

            .search-container {
                flex-direction: column;
                align-items: stretch;
            }

            .table-responsive {
                overflow-x: auto;
            }
        }
    </style>
</head>
<body>
<div class="header">
    <div class="logo">
        <i class="fas fa-store"></i>
        <h1>测盟汇系统</h1>
    </div>
    <div class="user-info">
        <i class="fas fa-user-shield"></i>
        <span>超级管理员：<span th:text="${session.user.username}"></span></span>
        <a th:href="@{/user/logout}" class="logout-btn">
            <i class="fas fa-sign-out-alt"></i>
            退出登录
        </a>
    </div>
</div>

<div class="container">
    <div class="content-card">
        <div class="action-bar">
            <div>
                <a href="/admin/dashboard" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> 返回首页
                </a>
            </div>
            <h2 class="page-title">
                <i class="fas fa-newspaper"></i> 动态管理
            </h2>
            <a href="/admin_add" class="btn btn-dynamic">
                <i class="fas fa-plus"></i> 发布新动态
            </a>
        </div>

        <div class="search-container">
            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-search"></i></span>
                <input type="text" id="keyword" class="form-control" placeholder="搜索关键字">
            </div>

            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-user"></i></span>
                <input type="text" id="author" class="form-control" placeholder="作者">
            </div>

            <button class="btn btn-primary" onclick="searchDynamics()">
                <i class="fas fa-search"></i> 搜索
            </button>
            <button class="btn btn-secondary" onclick="resetSearch()">
                <i class="fas fa-sync-alt"></i> 重置
            </button>

            <div class="btn-group">
                <button class="btn btn-outline-warning" onclick="showStatus(0)">
                    <i class="fas fa-hourglass-half"></i> 待审核
                </button>
                <button class="btn btn-outline-success" onclick="showStatus(1)">
                    <i class="fas fa-check-circle"></i> 已发布
                </button>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table">
                <thead>
                <tr>
                    <th style="width: 80px;">ID</th>
                    <th style="width: 100px;">封面</th>
                    <th>标题</th>
                    <th style="width: 120px;">作者</th>
                    <th style="width: 160px;">发布时间</th>
                    <th style="width: 100px;">状态</th>
                    <th style="width: 150px;">操作</th>
                </tr>
                </thead>
                <tbody id="dynamicList">
                </tbody>
            </table>
        </div>

        <div class="pagination" id="pagination">
        </div>
    </div>
</div>

<script>
    let currentPage = 1;
    const pageSize = 10;
    let currentStatus = null;

    $(document).ready(function() {
        loadDynamics();
    });

    function loadDynamics() {
        const keyword = $('#keyword').val().trim();
        const author = $('#author').val().trim();

        let data = {
            page: currentPage,
            pageSize: pageSize,
            keyword: keyword,
            author: author
        };

        if (currentStatus !== null) {
            data.status = currentStatus;
        }

        $.ajax({
            url: '/api/admin/dynamics',
            method: 'GET',
            data: data,
            success: function(response) {
                if (response.code === 200) {
                    renderDynamicList(response.data.list);
                    renderPagination(response.data.totalPages);
                } else {
                    alert('加载失败：' + response.message);
                }
            },
            error: function() {
                alert('服务器异常，请稍后重试');
            }
        });
    }

    function renderDynamicList(dynamics) {
        let html = '';
        if (dynamics.length === 0) {
            html = `
                <tr>
                    <td colspan="7" class="empty-state">
                        <i class="fas fa-inbox"></i>
                        <div>暂无符合条件的动态</div>
                    </td>
                </tr>
            `;
        } else {
            dynamics.forEach(dynamic => {
                const statusClass = dynamic.status === 0 ? 'status-pending' : 'status-published';
                const statusIcon = dynamic.status === 0 ? 'fas fa-hourglass-half' : 'fas fa-check-circle';
                const statusText = dynamic.status === 0 ? '待审核' : '已发布';
                const coverUrl = dynamic.coverUrl || 'https://via.placeholder.com/80x60?text=No+Image';

                html += `
                    <tr>
                        <td>${dynamic.id}</td>
                        <td><img src="${coverUrl}" class="cover-image" alt="封面"></td>
                        <td><a href="/admin_detail?id=${dynamic.id}" class="text-dark">${dynamic.title}</a></td>
                        <td>${dynamic.author || '未知'}</td>
                        <td>${formatDate(dynamic.publishTime)}</td>
                        <td><span class="status-tag ${statusClass}"><i class="${statusIcon}"></i>${statusText}</span></td>
                        <td>
                            <div class="action-buttons">
                                <a href="/admin_edit?id=${dynamic.id}" class="btn btn-primary btn-sm">
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                                <button class="btn btn-danger btn-sm" onclick="deleteDynamic(${dynamic.id})">
                                    <i class="fas fa-trash"></i> 删除
                                </button>
                            </div>
                        </td>
                    </tr>
                `;
            });
        }
        $('#dynamicList').html(html);
    }

    function renderPagination(totalPages) {
        let html = '';

        // 上一页
        html += `
            <li class="page-item ${currentPage === 1 ? 'disabled' : ''}">
                <a class="page-link" href="#" onclick="changePage(${currentPage - 1})">
                    <i class="fas fa-chevron-left"></i>
                </a>
            </li>
        `;

        // 页码
        const maxVisiblePages = 5;
        let startPage = Math.max(1, currentPage - Math.floor(maxVisiblePages / 2));
        let endPage = Math.min(totalPages, startPage + maxVisiblePages - 1);

        if (endPage - startPage + 1 < maxVisiblePages) {
            startPage = Math.max(1, endPage - maxVisiblePages + 1);
        }

        if (startPage > 1) {
            html += `
                <li class="page-item">
                    <a class="page-link" href="#" onclick="changePage(1)">1</a>
                </li>
                ${startPage > 2 ? '<li class="page-item disabled"><span class="page-link">...</span></li>' : ''}
            `;
        }

        for (let i = startPage; i <= endPage; i++) {
            html += `
                <li class="page-item ${currentPage === i ? 'active' : ''}">
                    <a class="page-link" href="#" onclick="changePage(${i})">${i}</a>
                </li>
            `;
        }

        if (endPage < totalPages) {
            html += `
                ${endPage < totalPages - 1 ? '<li class="page-item disabled"><span class="page-link">...</span></li>' : ''}
                <li class="page-item">
                    <a class="page-link" href="#" onclick="changePage(${totalPages})">${totalPages}</a>
                </li>
            `;
        }

        // 下一页
        html += `
            <li class="page-item ${currentPage === totalPages ? 'disabled' : ''}">
                <a class="page-link" href="#" onclick="changePage(${currentPage + 1})">
                    <i class="fas fa-chevron-right"></i>
                </a>
            </li>
        `;

        $('#pagination').html(html);
    }

    function changePage(page) {
        if (page < 1) return;
        currentPage = page;
        loadDynamics();
    }

    function showStatus(status) {
        currentStatus = status;
        currentPage = 1;
        loadDynamics();
    }

    function searchDynamics() {
        currentPage = 1;
        loadDynamics();
    }

    function resetSearch() {
        $('#keyword').val('');
        $('#author').val('');
        currentStatus = null;
        currentPage = 1;
        loadDynamics();
    }

    function deleteDynamic(id) {
        if (confirm('确定要删除这条动态吗？删除后不可恢复！')) {
            $.ajax({
                url: '/dynamic/delete',
                method: 'POST',
                data: { dynamicId: id },
                success: function(response) {
                    if (response.code === 200) {
                        alert('删除成功');
                        loadDynamics();
                    } else {
                        alert('删除失败：' + response.message);
                    }
                },
                error: function() {
                    alert('服务器异常，请稍后重试');
                }
            });
        }
    }

    function formatDate(dateStr) {
        const date = new Date(dateStr);
        return date.toLocaleString('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit'
        });
    }
</script>
</body>
</html>